import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/preferred_size.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:pocketfile/common/base/base_view.dart';
import 'package:pocketfile/common/pub_widget/pub_widget.dart';
import 'package:pocketfile/module/home/controller/home_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pocketfile/module/home/widget/device_widget.dart';
import 'package:pocketfile/module/home/widget/easy_refresh_widget.dart';
import 'package:pocketfile/values/app_colors.dart';

class HomePage extends BaseView<HomeController> {
  @override
  PreferredSizeWidget? appBar(BuildContext context) {
    // TODO: implement appBar
    return null;
  }

  @override
  Widget body(BuildContext context) {
    // TODO: implement body
    return Obx(() => controller.checkPermission
        ? _getPermissionWidget(context)
        : _getNoPermissionWidget(context));
  }

  Widget _getNoPermissionWidget(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            "请允许相关权限",
            style: TextStyle(fontSize: 25.w),
          ),
          GestureDetector(
            onTap: () => AppSettings.openAppSettings(),
            child: Text(
              "去设置",
              style: TextStyle(
                fontSize: 25.w,
                color: Colors.amber,
                decoration: TextDecoration.underline,
                decorationStyle: TextDecorationStyle.solid,
                decorationThickness: 1,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _getPermissionWidget(BuildContext context) {
    return EasyRefresh(
      enableControlFinishRefresh: false,
      controller: controller.refController,
      header: EmptyHeader(),
      // onRefresh: () async {
      //   controller.updateHomeData();
      // },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          LeftTitleBar("口袋文件"),
          // FloatingActionButton(onPressed: () {
          //   controller.removeBleDevice();
          // }),
          Obx(
            () => Container(
                color: AppColors.pageBackground,
                width: MediaQuery.of(context).size.width,
                // 屏幕宽度
                height: MediaQuery.of(context).size.height,
                // 屏幕高度
                padding: const EdgeInsets.fromLTRB(30, 15, 0, 0),
                child: Wrap(
                  spacing: 20,
                  runSpacing: 30,
                  children: controller.bleDevicesList
                      .map((item) => DeviceWidget(item))
                      .toList()
                      .cast<Widget>(),
                )),
          )
        ],
      ),
    );
    ;
  }

  @override
  Color pageBackgroundColor() {
    // TODO: implement pageBackgroundColor
    return Colors.white;
  }
}
